<?php
/* ------- 数组递归取值 -------- */
function array_values_recursive($array) 
{
   $flat = array();
   foreach ($array as $value) 
	 {
		if (is_array($value)) $flat = array_merge($flat, array_values_recursive($value));
		else $flat[] = $value;
	 }
   return $flat;
}

/* ------- 组合平整数组 ------- */
function array_group_and_flat($array)
{
   $array = array_values_recursive($array);
   return array_unique($array);
}

/* ------- 从一个数组去掉另一个数组所拥有的值 ------- */
function array_del_from_another($array1, $array2)
{
   foreach ($array2 as $v)
	 {
		$index = array_search($v, $array1);
		if ($index !== FALSE)
		  {
			 unset($array1[$index]);
		  }
	 }
   return array_values($array1);
}

/* ------- 生成公司－部门－职位数组，并在 js 相应目录中保存为文件 data_company_position.js ------- */
function make_company_section_position()
{
	global $conf;
	$filepaths = $conf["filepaths"];
	$company = array();
   
	$sql = "SELECT a.company_id company_id, a.zh_name company_name, a.power company_power,
				b.section_id section_id, b.zh_name section_name, b.master_id master_id, b.power section_power, 
				c.position_id position_id, c.zh_name position_name, c.power position_power
			FROM company a, section b, position c
			WHERE a.company_id = b.company_id AND b.section_id = c.section_id
					 AND a.company_id<>0 AND b.section_id<>0 AND c.position_id<>0
			ORDER BY company_id, section_id, position_id ASC";
	$re = db_query($sql);

	// Produce datas into PHP multiple level Array
	foreach ($re as $v)
	{
		$c_id			= $v["company_id"];
		$c_name		= $v["company_name"];
		$c_power		= $v["company_power"];
		$s_id			= $v["section_id"];
		$s_name		= $v["section_name"];
		$s_power		= $v["section_power"];
		$s_masterid	= $v["master_id"];
		$p_id			= $v["position_id"];
		$p_name		= $v["position_name"];
		$p_power		= $v["position_power"];
		
		$c_temp = array(array(), $c_name, $c_power);
		$s_temp = array(array(), $s_name, $s_power, $s_masterid);
		$p_temp = array($p_name, $p_power);
		
		if (!isset($company[$c_id])) { $company[$c_id] = $c_temp; }
		if (!isset($company[$c_id][0][$s_id])) { $company[$c_id][0][$s_id] = $s_temp; }
		if (!isset($company[$c_id][0][$s_id][0][$p_id])) { $company[$c_id][0][$s_id][0][$p_id] = $p_temp; }
	}
	
	// Convert PHP Array into javascript code
   	$hash_1 = array();
	foreach ($company as $k1 => $v1)
	{
		$str_1 = "'" . $k1 . "':[{";
		$hash_2 = array();
		foreach ($v1[0] as $k2 => $v2)
		{
			$str_2 = "'" . $k2 . "':[{";
			$hash_3 = array();
			foreach ($v2[0] as $k3 => $v3)
			{
				$str_3 = "'" . $k3 . "':['" . $v3[0] . "','" . $v3[1] . "']";
				$hash_3[] = $str_3;
			}
			$str_2 .= implode(",", $hash_3) . "},'" . $v2[1] . "','" . $v2[2] . "','" . $v2[3] . "']";
			$hash_2[] = $str_2;
		}
		$str_1 .= implode(",", $hash_2) . "},'" . $v1[1] . "','" . $v1[2] . "']";
		$hash_1[] = $str_1;
	}
	$last_str = "var __company_2_position__ = {" . implode(",", $hash_1) . "};";
	
	// Write string to file
	$filename = "data_company_position.js";
	$handle = fopen($filepaths["oa_data_js"] . $filename, "w");
	$flag = false;
	
	if (!fwrite($handle, $last_str)) { $flag = false; } else { $flag = true; }
	fclose($handle);
	
	return $flag;
}

/* 写入日志 */
function write_event_to_log($userid, $suc_status, $ulogs, $escape_flag)
{
	$sql = ($escape_flag)
			? ("INSERT INTO sys_log(user_id, suc_status, ulogs, log_time) VALUES('" . $userid . "', " . $suc_status . ", '" . mysql_escape_string($ulogs) . "', NOW())")
			: ("INSERT INTO sys_log(user_id, suc_status, ulogs, log_time) VALUES('" . $userid . "', " . $suc_status . ", '" . $ulogs . "', NOW())");
	return db_exec($sql);
}

/* 判断数据提交情况并写入日志 */
function check_status_write_to_log($status, $log, $escape_flag)
{
	$alert_str = array("Success: Submit datas successful!", "Error: Fail on datas submition!");
	$suc_status = (!$status) ? 0 : 1;
	if ($status)
	{
		write_event_to_log($_SESSION["user"]["id"], $suc_status, $log[0], $escape_flag);
		return $alert_str[0];
	}
	else
	{
		write_event_to_log($_SESSION["user"]["id"], $suc_status, $log[1], $escape_flag);
		return $alert_str[1];
	}
}
/* 反转义字符串 */
function unescape_string($str)
{
	$search = array("\\\"", "\\\'", "\\\\");
	$replace = array("\"", "\'", "\\");
	return str_replace($search, $replace, $str);
}
?>
